﻿@page "/xlsio/create-excel"
@inject Microsoft.JSInterop.IJSRuntime JS
@using System.IO;

@using Syncfusion.Blazor.Buttons
@*Hidden:Lines*@

@using BlazorDemos.Data.FileFormats.XlsIO

@*End:Hidden*@
@inherits SampleBaseComponent;

<SampleDescription>
  <p>This sample demonstrates how to create a simple spreadsheet document using XlsIO.</p>  
</SampleDescription>
<ActionDescription>
   <p>In this sample, a new instance of XlsIO is created. This instance has no open workbooks. A workbook is created with the IWorkbook interface. This workbook has one worksheet by default and number of worksheets can be created based on the need. The IRange interface can be used to access the cell and write the content.</p>
<p style='display: block'><b>Features:</b></p>
<ul>
<li>Creating workbooks from scratch in various file formats</li>
<li>Creating more numbers of sheets</li>
<li>Writing text in the range of cells</li>
</ul>
</ActionDescription>

<div class="control-section">
    <p style="font-weight:normal">Click the button to view an Excel spreadsheet generated by Essential XlsIO. Please note that Microsoft Excel Viewer or Microsoft Excel is required to view the resultant document.</p>
    
    <div class="radio-control">
        <h5 class="row">Save As:</h5>
        <div class="e-radio-wrapper">
            <SfRadioButton Label="XLS" Name="XLS" Value=@("XLS") @bind-Checked="@excelVersion"></SfRadioButton>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <div class="e-radio-wrapper">
            <SfRadioButton Label="XLSX" Name="XLSX" Value=@("XLSX") @bind-Checked="@excelVersion"></SfRadioButton>
        </div>
    </div>
    <div class="button-section">
        <div id="button-control">
            <div class="row">
                <div>
                    <SfButton @onclick="CreateDocument">Create Document</SfButton>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .radio-control {
        margin: 5% 0 2% 0;
    }    

    .control-section .row {
        margin: 10px 0;
    }
</style>

@code {

    MemoryStream excelStream;
    string excelVersion = "XLSX";

    /// <summary>
    /// Create and download the simple Excel document
    /// </summary>
    public async Task CreateDocument()
    {
        CreateService service = new CreateService();
        excelStream = service.CreateXlsIO(excelVersion);
        if (excelVersion == "XLSX")
        {
            await JS.SaveAs("Sample.xlsx", excelStream.ToArray());
        }
        else
        {
            await JS.SaveAs("Sample.xls", excelStream.ToArray());
        }
    }
}